<?php
require_once '../inc/configuration.php';
require_once '../inc/connexion_bdd.php';
require_once '../inc/fonctions_sql.php';

$message_action = "";

$action = isset($_POST["action"]) ? $action = $_POST["action"] : "";

if($action==1) {
    
    $tab_name_form = array(
        "id_contacts",
        "rdv_date",
        "heure",
        "type",
        "objet",
        "observations"
    );
    
    $message_action = ajouter_donnees("rendez_vous", $tab_name_form);
    
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Ajouter un RDV</title>
        <?php require_once "../inc/css_bootstrap.php" ?>
    </head>
    <body>
        <?php require_once '../inc/menu_principal.php'; ?>
        <h1>Ajouter un Rendez-Vous</h1>
        <?php if(!empty($message_action)) { ?>
            <div id="message_action" class="alert <?php echo $message_action["css_class"]; ?>"><?php echo $message_action["texte"]; ?></div>
        <?php } ?>
            
        <form id="id_form" name="form_ajouter_rdv" method="post" action="" enctype="multipart/form-data">
            <table class="table table-bordered">
                    <tr>
                        
                        <th>
                            Contact : 
                        </th>
                        <td>
                            <?php
                                $sql = "SELECT id, raison_sociale FROM entreprises ORDER BY raison_sociale ASC";
                                $envoi_requete = mysqli_query($connexion_serveur, $sql);

                            ?>
                            <select name="id_contacts" id="id_contacts">
                                
                            <?php
                            while($data = mysqli_fetch_assoc($envoi_requete)) {
                                $sql_contacts = "SELECT nom, prenom FROM contacts WHERE id_entreprise = ".$data["id"];
                                $req_contacts = mysqli_query($connexion_serveur, $sql_contacts);
                                while($data_contacts = mysqli_fetch_assoc($req_contacts)) {
                            ?>
                            <option value="<?php echo $data["id"]; ?>"><?php echo $data["raison_sociale"]; ?> >> <?php echo $data_contacts["nom"].' '.$data_contacts["prenom"]; ?></option>
                            <?php
                                }
                            }
                            ?>
                         </select>
                        </td>
                        </tr>

                    <tr>

                        <th>
                            Date : 
                        </th>
                        <td>
                          <input type="date" name="rdv_date" id="rdv_date" class="form-control" required>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Heure : 
                        </th>
                        <td>
                         <input type="time" name="heure" id="heure" class="form-control" required>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Type : 
                        </th>
                        <td>
                            <select name="type_rdv" id="type_rdv" class="form-control">
                                <?php
                                $tab_type_rdv = array("Autre", "Présentation produits", "Livraison échantillons");
                                for($x=0; $x<count($tab_type_rdv); $x++) {
                                ?>
                                <option><?php echo $tab_type_rdv[$x]; ?></option>
                                <?php
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>
                           Objet : 
                        </th>
                        <td>
                           <input type="text" name="objet" id="objet">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Observations : 
                        </th>
                        <td>
                            <textarea name="observations" id="observations" class="form-control"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <button type="submit" name="action" class="btn btn-success" value="1"><span class="glyphicon glyphicon-ok"></span>Ajouter</button>
                        </td>
                    </tr>
                </table>
        </form>
    </body>
</html>
